﻿using System.Collections.Generic;
using AutoMapper;

namespace NGeo.Yahoo.GeoPlanet.Json
{
    public static class JsonConverter
    {
        static JsonConverter()
        {
            Configure();
        }

        public static void Configure()
        {
            Mapper.CreateMap<JsonPlace, Place>()
                .ForMember(target => target.Type, opt => opt.ResolveUsing(source =>
                    (source.Type != null || !string.IsNullOrWhiteSpace(source.TypeName)) ? new PlaceType
                    {
                        Name = source.TypeName,
                        Code = (source.Type != null) ? source.Type.Code : 0,
                    } : null))
                .ForMember(target => target.Country, opt => opt.ResolveUsing(source =>
                    (source.Country != null || !string.IsNullOrWhiteSpace(source.CountryName)) ? new Admin
                    {
                        Name = source.CountryName,
                        Code = (source.Country != null) ? source.Country.Code : null,
                        Type = (source.Country != null) ? source.Country.Type : null,
                    } : null))
                .ForMember(target => target.Admin1, opt => opt.ResolveUsing(source =>
                    (source.Admin1 != null || !string.IsNullOrWhiteSpace(source.Admin1Name)) ? new Admin
                    {
                        Name = source.Admin1Name,
                        Code = (source.Admin1 != null) ? source.Admin1.Code : null,
                        Type = (source.Admin1 != null) ? source.Admin1.Type : null,
                    } : null))
                .ForMember(target => target.Admin2, opt => opt.ResolveUsing(source =>
                    (source.Admin2 != null || !string.IsNullOrWhiteSpace(source.Admin2Name)) ? new Admin
                    {
                        Name = source.Admin2Name,
                        Code = (source.Admin2 != null) ? source.Admin2.Code : null,
                        Type = (source.Admin2 != null) ? source.Admin2.Type : null,
                    } : null))
                .ForMember(target => target.Admin3, opt => opt.ResolveUsing(source =>
                    (source.Admin3 != null || !string.IsNullOrWhiteSpace(source.Admin3Name)) ? new Admin
                    {
                        Name = source.Admin3Name,
                        Code = (source.Admin3 != null) ? source.Admin3.Code : null,
                        Type = (source.Admin3 != null) ? source.Admin3.Type : null,
                    } : null))
                .ForMember(target => target.Locality1, opt => opt.ResolveUsing(source =>
                    (source.Locality1 != null || !string.IsNullOrWhiteSpace(source.Locality1Name)) ? new Locality
                    {
                        Name = source.Locality1Name,
                        Type = (source.Locality1 != null) ? source.Locality1.Type : null,
                    } : null))
                .ForMember(target => target.Locality2, opt => opt.ResolveUsing(source =>
                    (source.Locality2 != null || !string.IsNullOrWhiteSpace(source.Locality2Name)) ? new Locality
                    {
                        Name = source.Locality2Name,
                        Type = (source.Locality2 != null) ? source.Locality2.Type : null,
                    } : null))
            ;

            Mapper.CreateMap<JsonPlaceType, PlaceType>()
                .ForMember(target => target.Code, opt => opt.MapFrom(source =>
                    (source.JsonAttributes != null) ? source.JsonAttributes.Code : 0));
        }

        public static Place ToPlace(this JsonPlace jsonPlace)
        {
            return Mapper.Map<Place>(jsonPlace);
        }

        public static List<Place> ToPlaces(this List<JsonPlace> jsonPlaces)
        {
            return Mapper.Map<List<Place>>(jsonPlaces);
        }

        public static List<PlaceType> ToPlaceTypes(this List<JsonPlaceType> jsonPlaceTypes)
        {
            return Mapper.Map<List<PlaceType>>(jsonPlaceTypes);
        }

    }
}
